package com.company.zoj;

import java.util.Scanner;

/**
 * Created by v on 17-6-4.
 */
public class ZojTest3533 {
    static StringBuffer str;

    private static void lcp(int first,int second){
        String str1=str.substring(first-1);
        String str2=str.substring(second-1);

        int current=0;
        while (current<str1.length()&&current<str2.length()){
            if(str1.charAt(current)==str2.charAt(current)){
                current++;
            }else {
                break;
            }
        }

        System.out.println(current);
    }

    private static void palindrome(){
        int max=0;
        //偶回文
        for(int i=1;i<str.length();i++){
            int low=i-1;
            int hight=i;
            while (low>=0&&hight<str.length()){
                if(str.charAt(low)==str.charAt(hight)){
                    low--;
                    hight++;
                }else {
                    break;
                }
            }
            max=Math.max(hight-low-1,max);
        }

        //奇回文
        for(int i=1;i<str.length();i++){
            int low=i-1;
            int hight=i+1;
            while (low>=0&&hight<str.length()){
                if(str.charAt(low)==str.charAt(hight)){
                    low--;
                    hight++;
                }else {
                    break;
                }
            }
            max=Math.max(hight-low-1,max);
        }

        System.out.println(max);
    }

    private static void reverse(int a,int b){
        char ch=str.charAt(a-1);
        str.setCharAt(a-1,str.charAt(b-1));
        str.setCharAt(b-1,ch);
    }

    private static void modify(int index,char ch){
        str.setCharAt(index-1,ch);
    }

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        try {
            while (true){
                str=new StringBuffer(input.next());
                if(str.length()==0){
                    return;
                }
                int n=input.nextInt();
                for(int i=0;i<n;i++){
                    String instruction=input.next();

                    switch (instruction){
                        case "Lcp":{
                            int a=input.nextInt();
                            int b=input.nextInt();
                            lcp(a,b);
                            break;
                        }
                        case "Palindrome":{
                            palindrome();
                            break;
                        }
                        case "Reverse":{
                            int a=input.nextInt();
                            int b=input.nextInt();
                            reverse(a,b);
                            break;
                        }
                        case "Modify":{
                            int a=input.nextInt();
                            char b=input.next().charAt(0);
                            modify(a,b);
                            break;
                        }
                    }
                }
            }
        }catch (Exception ex){
            return;
        }
    }
}
